let logger = require("pomelo-logger").getLogger(__filename);
let uuid = require('node-uuid');
let DateUtil = require("../../../../utils/dateUtil");
let COMPONENT_NAME = "accountComponent";

function AccountComponent(owner) {
    this.owner = owner;
    this.dbId = null;
    this.uid = 0;
    this.channelUid = "";
    this.avatar = "";
    this.nickname = "";
    this.userChannel = "unknown";
    this.unionid = "";
    this.gender = 0;
    this.description = "";
    this.token = "";
    this.createTokenTime = 0;
    this.refreshTokenTime = 0;
    this.createTime = 0;
    this.loginTime = 0;
    this.lastLogoutTime = 0;
}

AccountComponent.prototype.setDBId = function (dbId) {
    this.dbId = dbId;
};

AccountComponent.prototype.initGuest = function (uid) {
    let now = DateUtil.nowMS();
    this.uid = uid;
    this.channelUid = "" + uid;
    this.nickname = "guest" + uid;
    this.gender = 1;
    this.avatar = "";
    this.userChannel = "guest";
    this.unionid = "";
    this.token = uuid.v4();
    this.createTokenTime = now;
    this.refreshTokenTime = now;
    this.createTime = now;
    this.loginTime = now;
    this.lastLogoutTime = 0;
};

AccountComponent.prototype.initWechat = function (uid, wechat) {
    let now = DateUtil.nowMS();
    this.uid = uid;
    this.channelUid = wechat["openid"];
    this.nickname = wechat["nickname"];
    this.gender = parseInt(wechat["gender"]);
    this.avatar = wechat["avatar"];
    this.token = wechat["token"];
    this.userChannel = wechat["userChannel"];
    this.unionid = wechat["unionid"];
    this.createTokenTime = now;
    this.refreshTokenTime = now;
    this.createTime = now;
    this.loginTime = now;
    this.lastLogoutTime = 0;
};

AccountComponent.prototype.initWyw = function (uid, wywData) {
    let now = DateUtil.nowMS();
    this.uid = uid;
    this.channelUid = wywData["openid"];
    this.nickname = wywData["nickname"];
    this.gender = parseInt(wywData["sex"]);
    // this.avatar = wywData["avatar"];
    this.token = uuid.v4();
    this.unionid = wywData["unionid"] ? wywData["unionid"] : 0;
    // this.userChannel = wywData["userChannel"];
    this.userChannel = 'wyw';
    this.createTokenTime = now;
    this.refreshTokenTime = now;
    this.createTime = now;
    this.loginTime = now;
    this.lastLogoutTime = 0;
};

AccountComponent.prototype.setDescription = function (description) {
    this.description = description;
};

AccountComponent.prototype.getDescription = function () {
    return this.description;
};

AccountComponent.prototype.login = function () {
    this.loginTime = DateUtil.nowMS();
};

AccountComponent.prototype.logout = function () {
    this.lastLogoutTime = DateUtil.nowMS();
};

AccountComponent.prototype.getRefreshTokenTime = function () {
    return this.refreshTokenTime;
};

AccountComponent.prototype.getUid = function () {
    return this.uid;
};

AccountComponent.prototype.getChannelUid = function () {
    return this.channelUid;
};

AccountComponent.prototype.getUserChannel = function () {
    return this.userChannel;
};

AccountComponent.prototype.getToken = function () {
    return this.token;
};

AccountComponent.prototype.getUnionid = function () {
    return this.unionid;
};

AccountComponent.prototype.isTokenExpired = function () {
    let now = DateUtil.nowMS();
    return (now - this.refreshTokenTime > 5 * 24 * 3600 * 1000) ||
        (now - this.createTokenTime > 30 * 24 * 3600 * 1000);
};

AccountComponent.prototype.touchToken = function () {
    let now = DateUtil.nowMS();
    if (this.isTokenExpired()) {
        logger.info("refresh token. now=" + now
            + " prev refreshTokenTime=" + this.refreshTokenTime
            + " createTokenTime=" + this.createTokenTime
            + " uid=" + this.uid);
        this.token = uuid.v4();
        this.createTokenTime = now;
        this.refreshTokenTime = now;
    } else {
        this.refreshTokenTime = now;
    }
};

AccountComponent.prototype.updateWechatTokenTo = function (token) {
    let now = DateUtil.nowMS();
    logger.info("update token. now=" + now
        + " prev refreshTokenTime=" + this.refreshTokenTime
        + " createTokenTime=" + this.createTokenTime
        + " uid=" + this.uid);
    this.token = token;
    this.createTokenTime = now;
    this.refreshTokenTime = now;
};

AccountComponent.prototype.getNickname = function () {
    return this.nickname;
};

AccountComponent.prototype.getGender = function () {
    return this.gender;
};

AccountComponent.prototype.getAvatar = function () {
    return this.avatar;
};

AccountComponent.prototype.fromBSON = function (document) {
    this.dbId = document["_id"];
    this.uid = document["uid"];
    this.channelUid = document["channelUid"];
    this.avatar = document["avatar"];
    this.nickname = document["nickname"];
    this.gender = document["gender"];
    this.unionid = document["unionid"];
    this.userChannel = document["userChannel"];
    this.description = document["description"];
    this.createTime = document["createTime"];
    this.loginTime = document["loginTime"];
    this.lastLogoutTime = document["lastLogoutTime"];
    this.token = document["token"];
    this.createTokenTime = document["createTokenTime"];
    this.refreshTokenTime = document["refreshTokenTime"];
};

AccountComponent.prototype.buildBSON = function (document) {
    document["_id"] = this.dbId;
    document["uid"] = this.uid;
    document["channelUid"] = this.channelUid;
    document["userChannel"] = this.userChannel;
    document["avatar"] = this.avatar;
    document["nickname"] = this.nickname;
    document["gender"] = this.gender;
    document["unionid"] = this.unionid;
    document["description"] = this.description;
    document["createTime"] = this.createTime;
    document["loginTime"] = this.loginTime;
    document["lastLogoutTime"] = this.lastLogoutTime;
    document["token"] = this.token;
    document["createTokenTime"] = this.createTokenTime;
    document["refreshTokenTime"] =this.refreshTokenTime;
};

AccountComponent.prototype.toJSON = function () {
    return {
        uid: this.uid,
        avatar: this.avatar,
        unionid: this.unionid,
        userChannel: this.userChannel,
        gender: this.gender,
        channelUid: this.channelUid,
        description: this.description,
        nickname: this.nickname
    };
};

module.exports.create = function (owner) {
    return new AccountComponent(owner);
};

